package com.springapp.mvc;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.springapp.mvc.pojo.Student;
import com.springapp.mvc.service.LoginService;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.Principal;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/test")
public class HelloController {

	private static final Logger LOG = Logger.getLogger(HelloController.class);

	@Resource(name = "loginServieImpl")
	private LoginService loginService;

	/**
	 * jsp视图返回
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/printWelcome", method = RequestMethod.GET)
	public String printWelcome(ModelMap model) {
		LOG.info("demo-web>>>>>>>>>>>>>>>>>>>>printWelcome.method");
		model.addAttribute("message", "Hello world!123456何胜金");
		return "hello";
	}

	/**
	 * 
	 * @Description:单点登录测试
	 * @author:hsj qq:2356899074
	 * @time:2017年8月16日 下午5:12:21
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/index.do")
	public String showIndex(HttpServletRequest request, HttpServletResponse response) {
		Principal principal = request.getUserPrincipal();
		request.getSession().setAttribute("message", principal.getName());
		return "hello";
	}

	/**
	 * 
	 * @Description:重定向测试
	 * @author:hsj qq:2356899074
	 * @time:2017年8月16日 下午5:12:03
	 * @param request
	 * @param response
	 */
	@RequestMapping("/index.doRedirect")
	public void doRedirect(HttpServletRequest request, HttpServletResponse response) {
		try {
			response.sendRedirect("http://www.ssoclient.com:8888/");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@RequestMapping("/ssoresult.do")
	public void ssoResult(HttpServletRequest request, HttpServletResponse response) {
		String ret = request.getParameter("ret");
		String msg = request.getParameter("msg");
		if (ret == null) {
			ret = "0";
		}
		String result = "<html><head><script language='javascript'>" + "parent.logincallback({'ret':" + ret + ",'msg':'"
				+ msg + "'});" + "</script></head> </html>";
		response.setContentType("text/html;charset=UTF-8");
		try {
			PrintWriter out = response.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (Exception e) {
		}
	}

	/**
	 * json对象数据返回给前端的ajax调用
	 * 
	 * @return
	 */
	@RequestMapping(value = "/query", method = RequestMethod.GET)
	@ResponseBody
	public Student query() {
		LOG.info("demo-web>>>>>>>>>>>>>>>>>>>>query.method");
		Student student = loginService.query();
		System.out.println(student.getName() + " 性别： " + student.getSex());
		return student;
	}
}